package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.fx.employee.entity.FxCompanyinfo;
import com.vivo.fx.employee.service.FxCompanyinfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Component
public class TaskCompanyInfo {

    private Logger logger = Logger.getLogger(TaskCompanyInfo.class);
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private OfficeService officeService;


    @Autowired
    private FxCompanyinfoService fxCompanyinfoService;

    @Autowired
    private AreaService areaService;

    public void getData() {
        //TODO 获取安徽数据库数据
        getDataFromAhDB();

        //TODO 获取江苏数据库数据
        getDataFromJsDB();
    }

    private void getDataFromRemoteDB(String area) {
        boolean isAh = area.equals("ah");
        System.out.println("get data from " + area + " db start...");

        List<FxCompanyinfo> companyinfos = null;
        Page<FxCompanyinfo> pageRes = null;

        try {
            // 切换数据库
            Helper.changeDB(area);
//            companyinfos = fxCompanyinfoService.findList(new FxCompanyinfo());

            int pageStart = 1;
            Page<FxCompanyinfo> page = new Page<FxCompanyinfo>(pageStart, 5000);
            page.setOrderBy("companycode");

            pageRes = fxCompanyinfoService.findPage(page, new FxCompanyinfo());

        } finally {
            DatabaseContextHolder.setMainDb();
        }

        Area curArea = null;
        if (area.equals("ah")) {
            curArea = areaService.get("e90e62912ea749d7bad38abfcae20d54");
        } else {
            curArea = areaService.get("ad48fa845d31437799f33140e51cfee6");
        }

//        Area curArea = null;

        for (FxCompanyinfo companInfo: pageRes.getList()) {
            // 查询该组织机构是否已经添加
            Office office = officeService.get(companInfo.getCompanyid());
            if (office == null) {
                System.out.println("添加组织机构入库：" + companInfo.getCompanyname() + " with id: " + companInfo.getCompanyid());

                office = new Office();

//                // 1 为分公司信息
//                if (companInfo.getCompanytype().equals("1")) {
//                    curArea = new Area();
////                    curArea.setIsTaskInsert(true);
//                    curArea.setCode(companInfo.getCompanycode());
//                    curArea.setType(companInfo.getCompanytype());
//                    curArea.setName(companInfo.getCompanyname());
//
//                    if (companInfo.getParentcompanyid() == null) {
//                        curArea.setParent(null);
//                    } else {
//                        Area faArea = areaService.get(companInfo.getParentcompanyid());
//                        curArea.setParent(faArea);
//                    }
//
//                    areaService.save(curArea);
//                }


                if (companInfo.getParentcompanyid() == null || companInfo.getParentcompanyid().equals("")) {
                    office.setParent(null);
                }else {
                    Office parent = officeService.get(companInfo.getParentcompanyid());
                    if (parent != null) {
                        office.setParent(parent);
                    } else {
                        office.setParent(null);
                    }
                }

                office.setIsTaskInsert(true);
                office.setIsNewRecord(true);

                office.setId(companInfo.getCompanyid());

                office.setName(companInfo.getCompanyname());
                office.setArea(curArea);
                office.setCode(companInfo.getCompanycode());
                office.setType(companInfo.getCompanytype());
                office.setGrade(companInfo.getCompanytype());
                office.setAddress(companInfo.getCompanyaddress());
                office.setMaster(companInfo.getPrincipalman());
                office.setPhone(companInfo.getContacttel());
                office.setEmail(companInfo.getEmail());
                office.setUseable("1");
                office.setIsOld("1");

                System.out.println(companInfo.getCompanytype());
                System.out.println(office.getGrade());

                System.out.println(companInfo.getCompanycode());

//                vivoCompanyInfoService.insert(newCompInfo);

                officeService.save(office);
            } else {
                // 修改组织机构信息，如添加附加类型，机构类别等


                System.out.println("该组织已经同步过：" + companInfo.getCompanyname() + " with id: " + companInfo.getCompanyid());
            }

        }

        System.out.println("get data from ah db end.");
    }

    private void getDataFromAhDB() {
        getDataFromRemoteDB("ah");
    }

    private void getDataFromJsDB() {
        getDataFromRemoteDB("js");
    }
}
